package StarRank;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

//数据：{"_id":{"$oid":"5ad89b396afaf881d81e6ce3"},"title":"快手枪手快枪手","year":"2016","type":"喜剧,动作","star":5.1,"director":"潘安子","actor":"林更新,张静初,腾格尔,刘晓庆,锦荣,曾江,施予斐,含笑,文淇","pp":16518,"time":115,"film_page":"https://movie.douban.com/subject/26219893/"}

public class MovieInfo implements WritableComparable<MovieInfo> {

    private String title;       //电影名称
    private int year;           //上映时间
    private String type;        //电影类型
    private float star;         //评分
    private String director;    //导演
    private String actor;       //演员
    private String time;        //时长
    private String film_page;   //链接

    public String toString() {
        return "MovieInfo [title=" + title + ", year=" + year + ", type=" + type + ", star=" + star +
                ", director=" + director + ", actor=" + actor + ", time=" + time + ", film_page=" + film_page + "]";
    }

    public int compareTo(MovieInfo mi) {
        //多个列的排序：select*from mi order by star,year;
        //首先按star评分排序
        if (this.star < mi.getStar()) {
            return 1;
        } else if (this.star > mi.getStar()) {
            return -1;
        }
        //如果star相等，则按照year上映时间排序
        if (this.year <= mi.getYear()) {
            return 1;
        } else{
            return -1;
        }
    }
    //反序列化方法：将可跨机器传输数据流（二进制串）转化为java对象的一种技术
    public void readFields(DataInput input) throws IOException {
        this.title = input.readUTF();
        this.year = input.readInt();
        this.type = input.readUTF();
        this.star = input.readFloat();
        this.director = input.readUTF();
        this.actor = input.readUTF();
        this.time = input.readUTF();
        this.film_page = input.readUTF();
    }
    //序列化方法：将java对象转化为可跨机器传输数据流（二进制串/字节）的一种技术
    public void write (DataOutput output) throws IOException {
        output.writeUTF(this.title);
        output.writeInt(this.year);
        output.writeUTF(this.type);
        output.writeFloat(this.star);
        output.writeUTF(this.director);
        output.writeUTF(this.actor);
        output.writeUTF(this.time);
        output.writeUTF(this.film_page);
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public float getStar() { return star; }
    public void setStar(float star) {
        this.star = star;
    }
    public String getDirector() {
        return director;
    }
    public void setDirector(String director) { this.director = director; }
    public String getActor() {
        return actor;
    }
    public void setActor(String actor) {
        this.actor = actor;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public String getFilm_page() {
        return film_page;
    }
    public void setFilm_page(String film_page) {
        this.film_page = film_page;
    }
}